#include "linechartwidget.h"

LineChartWidget::LineChartWidget(QWidget *parent) : QWidget(parent)
{
    // 创建图表
    m_chart = new QChart();
    m_chart->setTitle("实时数据趋势");
    m_chart->legend()->setVisible(true);
    m_chart->legend()->setAlignment(Qt::AlignBottom);

    // 创建三个数据系列
    m_lightSeries = new QLineSeries();
    m_lightSeries->setName("光照 (lux)");
    m_chart->addSeries(m_lightSeries);

    m_tempSeries = new QLineSeries();
    m_tempSeries->setName("温度 (°C)");
    m_chart->addSeries(m_tempSeries);

    m_humiditySeries = new QLineSeries();
    m_humiditySeries->setName("湿度 (%)");
    m_chart->addSeries(m_humiditySeries);

    // 设置不同颜色
    m_lightSeries->setColor(QColor(255, 165, 0)); // 橙色
    m_tempSeries->setColor(QColor(255, 0, 0));   // 红色
    m_humiditySeries->setColor(QColor(0, 0, 255)); // 蓝色

    // 创建坐标轴
    m_axisX = new QValueAxis();
    m_axisX->setLabelFormat("%.1f");
    m_axisX->setTitleText("时间");

    m_axisY = new QValueAxis();
    m_axisY->setTitleText("值");

    m_chart->addAxis(m_axisX, Qt::AlignBottom);
    m_chart->addAxis(m_axisY, Qt::AlignLeft);

    // 将系列附加到坐标轴
    m_lightSeries->attachAxis(m_axisX);
    m_lightSeries->attachAxis(m_axisY);

    m_tempSeries->attachAxis(m_axisX);
    m_tempSeries->attachAxis(m_axisY);

    m_humiditySeries->attachAxis(m_axisX);
    m_humiditySeries->attachAxis(m_axisY);

    // 设置坐标轴范围
    m_axisX->setRange(0, 10);
    m_axisY->setRange(0, 1500);

    // 创建图表视图
    m_chartView = new QChartView(m_chart, this);
    m_chartView->setRenderHint(QPainter::Antialiasing);
    m_chartView->setMinimumSize(300, 200);

    // 创建布局
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(m_chartView);
    layout->setContentsMargins(0, 0, 0, 0);
}


void LineChartWidget::updateChart(const DataPoint &data){
    // 添加新数据点
    m_lightSeries->append(data.x, data.light);
    m_tempSeries->append(data.x, data.temperature);
    m_humiditySeries->append(data.x, data.humidity);
    // 自动调整X轴范围，使图表滚动显示最新数据
    if (data.x > m_axisX->max())
    {
        m_axisX->setRange(data.x - 10, data.x);
    }
    // 动态调整Y轴范围以适应数据
    updateYAxisRange();
}


void LineChartWidget::updateYAxisRange(){
    // 计算所有数据点的最大值
    qreal maxValue = 0;
    // 获取光照数据的最大值
    for (int i = 0; i < m_lightSeries->count(); ++i) {
        if (m_lightSeries->at(i).x() >= m_axisX->min() && m_lightSeries->at(i).x() <= m_axisX->max()) {
            maxValue = qMax(maxValue, m_lightSeries->at(i).y());
        }
    }
    // 获取温度数据的最大值
    for (int i = 0; i < m_tempSeries->count(); ++i) {
        if (m_tempSeries->at(i).x() >= m_axisX->min() && m_tempSeries->at(i).x() <= m_axisX->max()) {
            maxValue = qMax(maxValue, m_tempSeries->at(i).y());
        }
    }
    // 获取湿度数据的最大值
    for (int i = 0; i < m_humiditySeries->count(); ++i) {
        if (m_humiditySeries->at(i).x() >= m_axisX->min() && m_humiditySeries->at(i).x() <= m_axisX->max()) {
            maxValue = qMax(maxValue, m_humiditySeries->at(i).y());
        }
    }
    // 设置Y轴范围，留出一些余量
    m_axisY->setRange(0, maxValue * 1.1);
}
